package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.Shop;
import com.quanyue.haircut.domain.entity.ShopServer;
import com.quanyue.haircut.shop.service.ShopServerService;
import com.quanyue.haircut.shop.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "店铺")
@RestController
@RequestMapping("/api")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopServerService shopServerService;

    @ApiOperation(value = "创建店铺")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/shop", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Shop> createShop(@RequestBody Shop shop,
                                           @ApiIgnore Principal principal) {
        return ServiceResult.success(shopService.createShop(shop, principal.getMerchantNo()));
    }

    @ApiOperation(value = "修改店铺")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/shop", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateShop(@RequestBody Shop shop) {
        shopService.updateShop(shop);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除店铺")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/shop", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteShop(@RequestParam Integer shopId) {
        shopService.deleteShop(shopId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "店铺列表")
    @GetMapping(value = "/shop", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <Shop>> shopList(@RequestParam Integer merchantNo, @ApiIgnore Principal principal) {
        if (merchantNo == 0) merchantNo = principal.getMerchantNo();
        return ServiceResult.success(shopService.findShop(merchantNo));
    }

    @ApiOperation(value = "创建店铺服务")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/shop/server", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> createShopServer(@RequestBody List <ShopServer> shopServers) {
        shopServerService.createShopServer(shopServers);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改店铺服务")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/shop/server", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateShopServer(@RequestBody ShopServer shopServer) {
        shopServerService.updateShopServer(shopServer);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除店铺服务")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/shop/server", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteShopServer(@RequestParam Integer shopServerId) {
        shopServerService.deleteShopServer(shopServerId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "店铺服务列表")
    @GetMapping(value = "/shop/server", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <ShopServer>> shopServerList(@RequestParam Integer shopId) {
        return ServiceResult.success(shopServerService.shopServerList(shopId));
    }
}
